package com.mach.platform.service;

import com.mach.platform.domain.iot.IotDevConfiguration;
import com.mach.platform.repository.IotDevConfigRepository;
import com.mach.platform.utils.UUIDUtils;
import com.mach.platform.utils.springdata.DynamicSpecifications;
import com.mach.platform.utils.springdata.SearchFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @Description:
 * @Package: com.mach.platform.service
 * @time 2018/11/23 0023 15:21
 */
@Service
public class IotDevConfigServiceImpl implements BaseService<IotDevConfiguration>{

	@Autowired
	private IotDevConfigRepository iotDevConfigRepository;

	/**
	 * 保存
	 *
	 * @param iotDevConfiguration 保存的实体
	 * @return T 返回保存后的实体
	 */
	@Transactional(rollbackFor = Exception.class)
	@Override
	public IotDevConfiguration save (IotDevConfiguration iotDevConfiguration) {
		return iotDevConfigRepository.saveAndFlush(iotDevConfiguration);
	}

	/**
	 * 插入，批量保存
	 *
	 * @param list 实体集合
	 * @return List<T>
	 */
	@Transactional(rollbackFor = Exception.class)
	@Override
	public List<IotDevConfiguration> insertObjInBatch (List<IotDevConfiguration> list) {
		if (list.get(0).getUuid() == null) {
			String uuid = UUIDUtils.generateUUID();
			list = list.stream().map(item -> {
				item.setUuid(uuid);
				return item;
			}).collect(Collectors.toList());
		} else {
			String uuid = list.get(0).getUuid();
			iotDevConfigRepository.deleteAllByUuid(uuid);
		}
		return iotDevConfigRepository.saveAll(list);
	}

	/**
	 * 更新
	 *
	 * @param iotDevConfiguration 跟新的实体
	 * @return T
	 */
	@Transactional(rollbackFor = Exception.class)
	@Override
	public IotDevConfiguration update (IotDevConfiguration iotDevConfiguration) {
		return iotDevConfigRepository.saveAndFlush(iotDevConfiguration);
	}

	/**
	 * 删除--批量删除
	 *
	 * @param ids 批量删除id
	 * @return int 影响数
	 */
	@Transactional(rollbackFor = Exception.class)
	@Override
	public int delete (Long[] ids) {
		return iotDevConfigRepository.deleteByIdIn(ids);
	}

	/**
	 * 获取列表数据---使用 com.mach.platform.utils.springdata  下的DynamicSpecifications 的
	 * 写法进行分页查询
	 *
	 * @param pageable 分页参数
	 * @param params   其他参数
	 * @return Page<T>
	 */
	@Override
	public Page<IotDevConfiguration> list (Pageable pageable, Map params) {
		Specification specification = DynamicSpecifications
				                              .bySearchFilter(SearchFilter.parse(params).values(), IotDevConfiguration.class);
		return iotDevConfigRepository.findAll(specification, pageable);
	}

	public Page<Map> list2 (Pageable pageable, String param) {
		Page<Map> maps = iotDevConfigRepository.loadDevConfigPageData(param, pageable);
		return maps;
	}

	/**
	 * 根据 对应的 uuid 获取对应的数据
	 * @param uuid
	 * @return
	 */
	public List<IotDevConfiguration> findAllByDevIdAndUuid (String uuid){

		return iotDevConfigRepository.findAllByUuidOrderBySortAsc(uuid);
	}

	/**
	 * 删除对应的设备ID 和 uuid的数据
	 * @param uuid
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	public int deleteAllByDevIdAndUuid (String uuid){
		return iotDevConfigRepository.deleteAllByUuid(uuid);
	}

	/**
	 * 获取单体 数据
	 *
	 * @param id 获取详情的id
	 * @return T
	 */
	@Override
	public IotDevConfiguration info (Long id) {
		return null;
	}
}
